package net.dl.ss.web;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.dl.ss.service.FinalProtocolManager;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

/**
 * @author yakovlev.yuriy@gmail.com
 * @since 12.8.2008
 */
@Controller
public class ReportsController {
	private FinalProtocolManager finalProtocolManager;

	@Autowired
	public ReportsController(FinalProtocolManager finalProtocolManager) {
		this.finalProtocolManager = finalProtocolManager;
	}

	@RequestMapping("/reports.htm")
	public ModelAndView reportsHandler(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		Map<String, Object> model = new HashMap<String, Object>();
		return new ModelAndView("reports", "model", model);
	}
	
	@RequestMapping("/finalProtocol.pdf")
	public ModelAndView handleFinalProtocol(HttpServletRequest request, HttpServletResponse response) throws Exception {
		return new ModelAndView("finalProtocol", finalProtocolManager.getModel());
	}

	@RequestMapping("/participants.pdf")
	public ModelAndView handleSimpleReport(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		Map<String, Object> model = new HashMap<String, Object>();
		return new ModelAndView("participants", model);
	}
	
	@RequestMapping("/participants.html")
	public ModelAndView handleSimpleReportHtml(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		Map<String, Object> model = new HashMap<String, Object>();
		return new ModelAndView("participantsHtml", model);
	}

	@RequestMapping("/participants.csv")
	public ModelAndView handleSimpleReportCsv(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		Map<String, Object> model = new HashMap<String, Object>();
		return new ModelAndView("participantsCsv", model);
	}

	@RequestMapping("/participants.xls")
	public ModelAndView handleSimpleReportExcel(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		Map<String, Object> model = new HashMap<String, Object>();
		return new ModelAndView("participantsExcel", model);
	}

	/*@RequestMapping("/participantsPost.pdf")
	public ModelAndView handleSimpleReportPost(HttpServletRequest request,
			HttpServletResponse response) throws Exception {

		String reportTitle = request.getParameter("reportTitle");
		Map model = getModel();
		model.put("ReportTitle", reportTitle);

		return new ModelAndView("participantsCompile", model);
	}

	@RequestMapping("/participantsPost.action")
	public ModelAndView handleSimpleReportPostAction(HttpServletRequest request,
			HttpServletResponse response) throws Exception {

		String reportTitle = request.getParameter("reportTitle");
		Map model = getModel();
		model.put("ReportTitle", reportTitle);

		return new ModelAndView("participantsCompile", model);
	}

	@RequestMapping("/exporterParameters.html")
	public ModelAndView handleExporterParameters(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
	  return new ModelAndView("htmlReport", getModel());
	}

	@RequestMapping("/participantsMulti.*")
	public ModelAndView handleSimpleReportMulti(HttpServletRequest request,
			HttpServletResponse response) throws Exception {

		String uri = request.getRequestURI();
		String format = uri.substring(uri.lastIndexOf(".") + 1);

		Map model = getModel();
		model.put("format", format);

		return new ModelAndView("participantsMulti", model);
	}

	@RequestMapping("/participantsCompile.pdf")
	public ModelAndView handleSimpleReportCompile(HttpServletRequest request,
			HttpServletResponse response) throws Exception {

		return new ModelAndView("participantsCompile", getModel());
	}*/

	/**
	 * Here two instances of <code>List</code> are added to the
	 * model. The second instance, <code>SubReportData</code> is
	 * configured to be passed into the engine as a parameter
	 * which is subsequently passed into a sub-report by the
	 * master report file. Behind the scenes this <code>List</code> is
	 * transformed into an instance of <code>JRDataSource</code>.
	 */
	/*@RequestMapping("/subReport.pdf")
	public ModelAndView handleSubReport(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		Map model = getModel();
		model.put("SubReportData", getProductData());
		return new ModelAndView("subReport", model);
	}*/

	/*private List getProductData() {
		List list = new ArrayList();
		for (int x = 0; x < 10; x++) {
			SubReportDataBean bean = new SubReportDataBean();
			bean.setId(x);
			bean.setName("Foo Bar");
			bean.setPrice(1.9f);
			bean.setQuantity(1.0f);

			list.add(bean);
		}
		return list;
	}*/
}